package org.kernely.test.settings;

import java.util.Hashtable;

import org.kernely.test.testbundle.TestBundle;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyPresenter;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;

/**
 * The SettingsPresenter class
 */
public class SettingsPresenter extends KernelyPresenter<SettingsView>{

	private final String BUNDLE_PROPERTIES = "BundleTestSettings";

	/**
	 * @see org.kenely.core.service.IKernelyPresenter#bind()
	 */
	@Override
	public final void bind() {
		this.view.getResult().setCaption("Actual property 1 = "+TestBundle.getBundleSettings().getProperty(BUNDLE_PROPERTIES, "property1")
				+", Actual property 2 = "+ TestBundle.getBundleSettings().getProperty(BUNDLE_PROPERTIES, "property2"));
		// Adding a listener to the save button
		this.view.getSetButton().addListener(new ClickListener(){
			private static final long serialVersionUID = 7871219904239488944L;
			/**
			 * Called when a ClickEvent is fired
			 */
			public void buttonClick (Button.ClickEvent event) {
				// Create the map which will contain bundle's properties
				Hashtable<String, String> properties = new Hashtable<String, String>();
				
				properties.put("property1", view.getSettings1().toString());
				properties.put("property2", view.getSettings2().toString());
				TestBundle.getBundleSettings().newProperties(BUNDLE_PROPERTIES, properties);
				view.getResult().setCaption("Property 1 = "+TestBundle.getBundleSettings().getProperty(BUNDLE_PROPERTIES, "property1")
						+", Property 2 = "+
						TestBundle.getBundleSettings().getProperty(BUNDLE_PROPERTIES, "property2"));
			}
		});

	}
}
